//
//  LXLogger.m
//  APMDemo
//
//  Created by Jack on 2020/7/20.
//  Copyright © 2020 LX. All rights reserved.
//

#import "LXLogger.h"
#import "LXLoggerConfig.h"

/**************************************************
LXLogFormatter： log 格式输出
**************************************************/

@interface LXLogFormatter : NSObject<DDLogFormatter>

@end

@implementation LXLogFormatter

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage{
    
    return [NSString stringWithFormat:@"%@(%lu): %@",logMessage.fileName,
            (unsigned long)logMessage.line,
            logMessage.message];
}

@end


/**************************************************
 LXLogger
 **************************************************/

@interface LXLogger ()

/** 本地日志输出 */
@property (nonatomic, strong) DDFileLogger *fileLogger;

@end

@implementation LXLogger

+ (void)load{
    //注册sdk
    [[LXLogger sharedInstance] startMonitor];
}

+ (instancetype)sharedInstance{
    static LXLogger *lxLogger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lxLogger = [[LXLogger alloc] init];
    });
    return lxLogger;
}

#pragma mark - 开始监听
- (void)startMonitor{
    
    // 添加DDASLLogger，你的日志语句将被发送到Xcode控制台
    if (@available(iOS 10.0,*)) {
        
        [DDOSLogger sharedInstance].logFormatter = [[LXLogFormatter alloc] init];
        [DDLog addLogger:[DDOSLogger sharedInstance]];
    }
    else{

        [DDTTYLogger sharedInstance].logFormatter = [[LXLogFormatter alloc] init];
        [DDLog addLogger:[DDTTYLogger sharedInstance]];
    }
    
    //日志文件
    [DDLog addLogger:self.fileLogger];
}

#pragma mark - 移除监听
- (void)stopMonitor{
    
    [DDLog removeLogger:[DDTTYLogger sharedInstance]];
    //移除日志文件
    [DDLog removeLogger:self.fileLogger];
}

#pragma mark - 类方法
#pragma mark 日志文件地址
+ (NSString *)loggerFolderPath{
    
    NSString *folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/Logs/"];
    return folderPath;
}
/*
#pragma mark 移除日志文件
+ (void)removeAllLogFiles{
    
    NSString *folderPath   = [self loggerFolderPath];
    NSArray *contentsArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil];
    
    for (NSString *fileName in contentsArray) {
        NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    }
}*/

#pragma mark - Getter 方法
- (DDFileLogger *)fileLogger{
    
    if (!_fileLogger) {
       
        DDFileLogger *fileLogger = nil;
#ifdef DEBUG
        // 添加DDFileLogger，你的日志语句将写入到一个文件中，默认路径在沙盒的Library/Caches/Logs/目录下，文件名为bundleid+空格+日期.log。
        fileLogger = [[DDFileLogger alloc] init];
        //2 hour rolling
        fileLogger.rollingFrequency = 60 * 60 * 2;
        //50MB文件大小
        fileLogger.maximumFileSize = 5 * 1024 * 1024;
        fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
        fileLogger.logFormatter = [[LXLogFormatter alloc] init];
        [DDLog addLogger:fileLogger];
    
#else
    
        fileLogger = [[DDFileLogger alloc] init];
        //72 hour rolling
        fileLogger.rollingFrequency = 60 * 60 * 24 * 3;
        //50MB文件大小
        fileLogger.maximumFileSize = 5 * 1024 * 1024;
        fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
        fileLogger.logFormatter = [[LXLogFormatter alloc] init];
        [DDLog addLogger:fileLogger];
    
#endif
        _fileLogger = fileLogger;
    }
    
    return _fileLogger;
}

@end
